﻿using System;
using System.Windows.Media;
///
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using ScheduleMVVM.Model.PersistentFramework;
using ScheduleMVVM.Model.RuntimeFramework;
using ScheduleMVVM.Model.Miscellaneous;
using ScheduleMVVM.Localization;

namespace ScheduleMVVM.ViewModel
{
    /// <summary>
    /// This class contains properties that a View can data bind to.
    /// <para>
    /// Use the <strong>mvvminpc</strong> snippet to add bindable properties to this ViewModel.
    /// </para>
    /// <para>
    /// You can also use Blend to data bind with the tool's support.
    /// </para>
    /// <para>
    /// See http://www.galasoft.ch/mvvm/getstarted
    /// </para>
    /// </summary>
    public class AddFavouriteViewModel : ViewModelBase
    {
        #region Binded properties
        public String ApplicationTitle
        {
            get { return GeneralResource.ApplicationTitle.ToUpper(); }
        }
        public String PageTitle
        {
            get { return GeneralResource.AddFavPageTitle.ToLower(); }
        }
        public String TitleFieldDescriptionString
        {
            get { return GeneralResource.FavouriteTitleString; }
        }
        public String Title
        { get; set; }
        public RelayCommand SaveFavourite
        {
            get
            {
                return new RelayCommand(delegate
                {
                    var op = new UpdateContextCommand()
                    {
                        Argument = LinkedFavourite.InnerValue
                    };
                    if (LinkedFavourite.InnerValue.IsSearch)
                        op.Action = UpdateContextCommandType.ADD;
                    else op.Action = UpdateContextCommandType.RENAME;
                    op.Argument.Title = Title;
                    op.Argument.IsSearch = false;
                    Messenger.Default.Send<UpdateContextCommand>(op);
                    Messenger.Default.Send<UpdateRouteViewCommand>(new UpdateRouteViewCommand(LinkedFavourite));
                    ((App)App.Current).RootFrame.GoBack();
                    EmptyFields();
                });
            }
        }
        public void EmptyFields()
        {
            Title = String.Empty;
            RaisePropertyChanged("Title");
        }
        #endregion

        public RouteEntryWrapper LinkedFavourite
        { get; private set; }
        /// <summary>
        /// Initializes a new instance of the AddFavouriteViewModel class.
        /// </summary>
        public AddFavouriteViewModel()
        {
            if (IsInDesignMode)
            {
                // Code runs in Blend --> create design time data.
            }
            else
            {
                MessengerInstance = GalaSoft.MvvmLight.Messaging.Messenger.Default;
                MessengerInstance.Register<UpdateRouteViewCommand>(this, UpdateViewHandler);
            }
        }
        private void UpdateViewHandler(UpdateRouteViewCommand message)
        {
            LinkedFavourite = message.NewEntry;
        }
        ////public override void Cleanup()
        ////{
        ////    // Clean own resources if needed

        ////    base.Cleanup();
        ////}
    }
}